package com.jvtd.baseProject.data;

import android.content.Context;

import com.jvtd.baseProject.bean.db.User;
import com.jvtd.baseProject.data.api.ApiHelper;
import com.jvtd.baseProject.data.db.DbHelper;
import com.jvtd.baseProject.data.sp.PreferencesHelper;
import com.jvtd.baseProject.di.ApplicationContext;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.Observable;

/**
 * Created by Administrator on 2017/10/16.
 * 数据管理实现类
 */
@Singleton
public class AppDbManager implements DbManager
{
  private Context mContext;
  private DbHelper mDbHelper;
  private ApiHelper mApiHelper;
  private PreferencesHelper mPreferencesHelper;

  @Inject
  public AppDbManager(@ApplicationContext Context context, DbHelper dbHelper,
                      ApiHelper apiHelper, PreferencesHelper preferencesHelper)
  {
    mContext = context;
    mDbHelper = dbHelper;
    mApiHelper = apiHelper;
    mPreferencesHelper = preferencesHelper;
  }

  @Override
  public boolean isLogin()
  {
    return mPreferencesHelper.isLogin();
  }

  @Override
  public void setLogin()
  {
    mPreferencesHelper.setLogin();
  }

  @Override
  public void clearLogin()
  {
    mPreferencesHelper.clearLogin();
  }

  @Override
  public boolean isStartGuidePager()
  {
    return mPreferencesHelper.isStartGuidePager();
  }

  @Override
  public void saveGuidePageState()
  {
    mPreferencesHelper.saveGuidePageState();
  }



  @Override
  public Observable<Boolean> saveCurrentUser(User user)
  {
    return mDbHelper.saveCurrentUser(user);
  }

  @Override
  public Observable<Integer> clearCurrentUser()
  {
    return mDbHelper.clearCurrentUser();
  }

  @Override
  public Observable<User> getCurrentUser()
  {
    return mDbHelper.getCurrentUser();
  }

  @Override
  public void setLoginOut()
  {
    this.clearCurrentUser().subscribe();
    this.clearLogin();
  }

  @Override
  public Context context()
  {
    return mContext;
  }
}